﻿Imports System.IO
Imports System.Runtime.InteropServices
Imports System.Security.Cryptography
Imports System.Text
Imports System.ComponentModel

Public Class EncriptarObj

#Region "Declaracion"


    Public Const DRIVE_UNKNOWN = &H0
    Public Const DRIVE_NO_ROOT_DIR = &H1
    Public Const DRIVE_REMOVABLE = &H2
    Public Const DRIVE_FIXED = &H3
    Public Const DRIVE_REMOTE = &H4
    Public Const DRIVE_CDROM = &H5
    Public Const DRIVE_RAMDISK = &H6


    <DllImport("kernel32.dll")> Public Shared Function GetDriveType() As System.UInt32
    End Function

    <DllImport("kernel32.dll")> Private Shared Function GetVolumeInformation(ByVal lpRootPathName As String, ByVal lpVolumeNameBuffer As StringBuilder, ByVal nVolumeNameSize As Integer, ByRef lpVolumeSerialNumber As Integer, ByRef lpMaximumComponentLength As Integer, ByRef lpFileSystemFlags As Integer, ByVal lpFileSystemNameBuffer As StringBuilder, ByVal nFileSystemNameSize As Integer) As Boolean
    End Function

#End Region

#Region "Funciones"

    Public Shared Function GetInformations(ByVal strRootPath As String) As String()
        Try

            Dim sbVolumeName As StringBuilder = New StringBuilder(256)
            Dim sbFileSystemName As StringBuilder = New StringBuilder(256)
            Dim nVolSerial As Integer = 0
            Dim nMaxCompLength As Integer = 0
            Dim nFSFlags As Integer = 0
            Dim result As String() = {"", "", "", "", ""}
            Dim bResult As Boolean = GetVolumeInformation(strRootPath, _
                                         sbVolumeName, _
                                         256, _
                                         nVolSerial, _
                                         nMaxCompLength, _
                                         nFSFlags, _
                                         sbFileSystemName, _
                                         256)

            If bResult Then
                result(0) = nVolSerial.ToString
            End If

            Return result

        Catch ex As Exception
            Throw New ReglaNegociosObjetosException(My.Resources.ResMensajes.ErrorGetInformations, ex)
        End Try
    End Function

    Public Shared Function EncryptString(ByVal InputString As String, ByVal SecretKey As String, Optional ByVal CyphMode As CipherMode = CipherMode.ECB) As String

        Try
            Using Des As New TripleDESCryptoServiceProvider()
                'Put the string into a byte array
                Dim InputbyteArray As Byte() = Encoding.UTF8.GetBytes(InputString)
                'Create the crypto objects, with the key, as passed in
                Using hashMD5 As New MD5CryptoServiceProvider()
                    Des.Key = hashMD5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(SecretKey))
                End Using
                Des.Mode = CyphMode
                Using ms As MemoryStream = New MemoryStream()
                    Using cs As CryptoStream = New CryptoStream(ms, Des.CreateEncryptor(), CryptoStreamMode.Write)
                        'Write the byte array into the crypto stream
                        '(It will end up in the memory stream)
                        cs.Write(InputbyteArray, 0, InputbyteArray.Length)
                        cs.FlushFinalBlock()
                    End Using
                    'Get the data back from the memory stream, and into a string
                    Dim ret As StringBuilder = New StringBuilder()
                    Dim b As Byte() = ms.ToArray
                    ms.Close()
                    Dim I As Integer
                    For I = 0 To UBound(b)
                        'Format as hex
                        ret.AppendFormat("{0:X2}", b(I))
                    Next
                    Return ret.ToString()
                End Using
            End Using

        Catch ex As CryptographicException
            Throw New ReglaNegociosObjetosException(My.Resources.ResMensajes.ErrorAlEncriptar, ex)
            Return ""
        End Try

    End Function

    Public Shared Function DecryptString(ByVal InputString As String, ByVal SecretKey As String, Optional ByVal CyphMode As CipherMode = CipherMode.ECB) As String
        Try
            If InputString = String.Empty Then
                Return ""
            Else
                Using Des As New TripleDESCryptoServiceProvider()
                    'Put the string into a byte array
                    Dim InputbyteArray(CType(InputString.Length / 2 - 1, Integer)) As Byte
                    'Create the crypto objects, with the key, as passed in
                    Using hashMD5 As New MD5CryptoServiceProvider()
                        Des.Key = hashMD5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(SecretKey))
                    End Using
                    Des.Mode = CyphMode
                    'Put the input string into the byte array
                    Dim X As Integer
                    For X = 0 To InputbyteArray.Length - 1
                        Dim IJ As Int32 = (Convert.ToInt32(InputString.Substring(X * 2, 2), 16))
                        Dim BT As New ByteConverter()
                        InputbyteArray(X) = New Byte()
                        InputbyteArray(X) = CType(BT.ConvertTo(IJ, GetType(Byte)), Byte)
                    Next
                    Using ms As MemoryStream = New MemoryStream()
                        Using cs As CryptoStream = New CryptoStream(ms, Des.CreateDecryptor(), CryptoStreamMode.Write)
                            'Flush the data through the crypto stream into the memory stream
                            cs.Write(InputbyteArray, 0, InputbyteArray.Length)
                            cs.FlushFinalBlock()
                        End Using
                        '//Get the decrypted data back from the memory stream
                        Dim ret As StringBuilder = New StringBuilder()
                        Dim B As Byte() = ms.ToArray
                        ms.Close()
                        Dim I As Integer
                        For I = 0 To UBound(B)
                            ret.Append(Chr(B(I)))
                        Next
                        Return ret.ToString()
                    End Using
                End Using
            End If

        Catch ex As Exception
            Throw New ReglaNegociosObjetosException(My.Resources.ResMensajes.ErrorAlDesencriptar, ex)
        End Try
    End Function

#End Region

End Class
